import os

import matplotlib.pyplot as plt
import numpy as np
from adjustText import adjust_text
from dateutil.utils import today
from numpy import sort
from openpyxl import Workbook, load_workbook
# y轴百分比、小数处理包
from matplotlib import ticker

# 显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']

wb = Workbook()
wb = load_workbook('直分图表数据py.xlsx', data_only=True)
sheet = wb['Sheet1']

DATE = [str(sheet['BE3'].value)[5:10], str(sheet['BA3'].value)[5:10], str(sheet['AW3'].value)[5:10],
        str(sheet['AS3'].value)[5:10], str(sheet['AO3'].value)[5:10], str(sheet['AK3'].value)[5:10],
        str(sheet['AG3'].value)[5:10], str(sheet['AC3'].value)[5:10], str(sheet['Y3'].value)[5:10],
        str(sheet['U3'].value)[5:10], str(sheet['Q3'].value)[5:10], str(sheet['M3'].value)[5:10],
        str(sheet['I3'].value)[5:10], str(sheet['E3'].value)[5:10]]

ZF_RATE_HN = [
    [sheet['BG6'].value, sheet['BC6'].value, sheet['AY6'].value, sheet['AU6'].value, sheet['AQ6'].value,
     sheet['AM6'].value, sheet['AI6'].value, sheet['AE6'].value, sheet['AA6'].value, sheet['W6'].value,
     sheet['S6'].value, sheet['O6'].value, sheet['K6'].value,
     sheet['G6'].value],
    [sheet['BG7'].value, sheet['BC7'].value, sheet['AY7'].value, sheet['AU7'].value, sheet['AQ7'].value,
     sheet['AM7'].value, sheet['AI7'].value, sheet['AE7'].value, sheet['AA7'].value, sheet['W7'].value,
     sheet['S7'].value, sheet['O7'].value, sheet['K7'].value,
     sheet['G7'].value],
    [sheet['BG8'].value, sheet['BC8'].value, sheet['AY8'].value, sheet['AU8'].value, sheet['AQ8'].value,
     sheet['AM8'].value, sheet['AI8'].value, sheet['AE8'].value, sheet['AA8'].value, sheet['W8'].value,
     sheet['S8'].value, sheet['O8'].value, sheet['K8'].value,
     sheet['G8'].value],
    [sheet['BG9'].value, sheet['BC9'].value, sheet['AY9'].value, sheet['AU9'].value, sheet['AQ9'].value,
     sheet['AM9'].value, sheet['AI9'].value, sheet['AE9'].value, sheet['AA9'].value, sheet['W9'].value,
     sheet['S9'].value, sheet['O9'].value, sheet['K9'].value,
     sheet['G9'].value],
    [sheet['BG10'].value, sheet['BC10'].value, sheet['AY10'].value, sheet['AU10'].value, sheet['AQ10'].value,
     sheet['AM10'].value, sheet['AI10'].value, sheet['AE10'].value, sheet['AA10'].value, sheet['W10'].value,
     sheet['S10'].value, sheet['O10'].value,
     sheet['K10'].value, sheet['G10'].value],
    [sheet['BG11'].value, sheet['BC11'].value, sheet['AY11'].value, sheet['AU11'].value, sheet['AQ11'].value,
     sheet['AM11'].value, sheet['AI11'].value, sheet['AE11'].value, sheet['AA11'].value, sheet['W11'].value,
     sheet['S11'].value, sheet['O11'].value,
     sheet['K11'].value, sheet['G11'].value],
    [sheet['BG12'].value, sheet['BC12'].value, sheet['AY12'].value, sheet['AU12'].value, sheet['AQ12'].value,
     sheet['AM12'].value, sheet['AI12'].value, sheet['AE12'].value, sheet['AA12'].value, sheet['W12'].value,
     sheet['S12'].value, sheet['O12'].value,
     sheet['K12'].value, sheet['G12'].value],
    [sheet['BG13'].value, sheet['BC13'].value, sheet['AY13'].value, sheet['AU13'].value, sheet['AQ13'].value,
     sheet['AM13'].value, sheet['AI13'].value, sheet['AE13'].value, sheet['AA13'].value, sheet['W13'].value,
     sheet['S13'].value, sheet['O13'].value,
     sheet['K13'].value, sheet['G13'].value],
    [sheet['BG14'].value, sheet['BC14'].value, sheet['AY14'].value, sheet['AU14'].value, sheet['AQ14'].value,
     sheet['AM14'].value, sheet['AI14'].value, sheet['AE14'].value, sheet['AA14'].value, sheet['W14'].value,
     sheet['S14'].value, sheet['O14'].value,
     sheet['K14'].value, sheet['G14'].value],
    [sheet['BG15'].value, sheet['BC15'].value, sheet['AY15'].value, sheet['AU15'].value, sheet['AQ15'].value,
     sheet['AM15'].value, sheet['AI15'].value, sheet['AE15'].value, sheet['AA15'].value, sheet['W15'].value,
     sheet['S15'].value, sheet['O15'].value,
     sheet['K15'].value, sheet['G15'].value],
    [sheet['BG16'].value, sheet['BC16'].value, sheet['AY16'].value, sheet['AU16'].value, sheet['AQ16'].value,
     sheet['AM16'].value, sheet['AI16'].value, sheet['AE16'].value, sheet['AA16'].value, sheet['W16'].value,
     sheet['S16'].value, sheet['O16'].value,
     sheet['K16'].value, sheet['G16'].value],
    [sheet['BG17'].value, sheet['BC17'].value, sheet['AY17'].value, sheet['AU17'].value, sheet['AQ17'].value,
     sheet['AM17'].value, sheet['AI17'].value, sheet['AE17'].value, sheet['AA17'].value, sheet['W17'].value,
     sheet['S17'].value, sheet['O17'].value,
     sheet['K17'].value, sheet['G17'].value],
    [sheet['BG18'].value, sheet['BC18'].value, sheet['AY18'].value, sheet['AU18'].value, sheet['AQ18'].value,
     sheet['AM18'].value, sheet['AI18'].value, sheet['AE18'].value, sheet['AA18'].value, sheet['W18'].value,
     sheet['S18'].value, sheet['O18'].value,
     sheet['K18'].value, sheet['G18'].value],
    [sheet['BG19'].value, sheet['BC19'].value, sheet['AY19'].value, sheet['AU19'].value, sheet['AQ19'].value,
     sheet['AM19'].value, sheet['AI19'].value, sheet['AE19'].value, sheet['AA19'].value, sheet['W19'].value,
     sheet['S19'].value, sheet['O19'].value,
     sheet['K19'].value, sheet['G19'].value],
    [sheet['BG20'].value, sheet['BC20'].value, sheet['AY20'].value, sheet['AU20'].value, sheet['AQ20'].value,
     sheet['AM20'].value, sheet['AI20'].value, sheet['AE20'].value, sheet['AA20'].value, sheet['W20'].value,
     sheet['S20'].value, sheet['O20'].value,
     sheet['K20'].value, sheet['G20'].value],
    [sheet['BG21'].value, sheet['BC21'].value, sheet['AY21'].value, sheet['AU21'].value, sheet['AQ21'].value,
     sheet['AM21'].value, sheet['AI21'].value, sheet['AE21'].value, sheet['AA21'].value, sheet['W21'].value,
     sheet['S21'].value, sheet['O21'].value,
     sheet['K21'].value, sheet['G21'].value],
    [sheet['BG22'].value, sheet['BC22'].value, sheet['AY22'].value, sheet['AU22'].value, sheet['AQ22'].value,
     sheet['AM22'].value, sheet['AI22'].value, sheet['AE22'].value, sheet['AA22'].value, sheet['W22'].value,
     sheet['S22'].value, sheet['O22'].value,
     sheet['K22'].value, sheet['G22'].value],
    [sheet['BG23'].value, sheet['BC23'].value, sheet['AY23'].value, sheet['AU23'].value, sheet['AQ23'].value,
     sheet['AM23'].value, sheet['AI23'].value, sheet['AE23'].value, sheet['AA23'].value, sheet['W23'].value,
     sheet['S23'].value, sheet['O23'].value,
     sheet['K23'].value, sheet['G23'].value],
]
ZF_RATE_XN = [
    [sheet['BG25'].value, sheet['BC25'].value, sheet['AY25'].value, sheet['AU25'].value, sheet['AQ25'].value,
     sheet['AM25'].value, sheet['AI25'].value, sheet['AE25'].value, sheet['AA25'].value, sheet['W25'].value,
     sheet['S25'].value, sheet['O25'].value,
     sheet['K25'].value, sheet['G25'].value],
    [sheet['BG26'].value, sheet['BC26'].value, sheet['AY26'].value, sheet['AU26'].value, sheet['AQ26'].value,
     sheet['AM26'].value, sheet['AI26'].value, sheet['AE26'].value, sheet['AA26'].value, sheet['W26'].value,
     sheet['S26'].value, sheet['O26'].value,
     sheet['K26'].value, sheet['G26'].value],
    [sheet['BG27'].value, sheet['BC27'].value, sheet['AY27'].value, sheet['AU27'].value, sheet['AQ27'].value,
     sheet['AM27'].value, sheet['AI27'].value, sheet['AE27'].value, sheet['AA27'].value, sheet['W27'].value,
     sheet['S27'].value, sheet['O27'].value,
     sheet['K27'].value, sheet['G27'].value],
    [sheet['BG28'].value, sheet['BC28'].value, sheet['AY28'].value, sheet['AU28'].value, sheet['AQ28'].value,
     sheet['AM28'].value, sheet['AI28'].value, sheet['AE28'].value, sheet['AA28'].value, sheet['W28'].value,
     sheet['S28'].value, sheet['O28'].value,
     sheet['K28'].value, sheet['G28'].value],
    [sheet['BG29'].value, sheet['BC29'].value, sheet['AY29'].value, sheet['AU29'].value, sheet['AQ29'].value,
     sheet['AM29'].value, sheet['AI29'].value, sheet['AE29'].value, sheet['AA29'].value, sheet['W29'].value,
     sheet['S29'].value, sheet['O29'].value,
     sheet['K29'].value, sheet['G29'].value],
    [sheet['BG30'].value, sheet['BC30'].value, sheet['AY30'].value, sheet['AU30'].value, sheet['AQ30'].value,
     sheet['AM30'].value, sheet['AI30'].value, sheet['AE30'].value, sheet['AA30'].value, sheet['W30'].value,
     sheet['S30'].value, sheet['O30'].value,
     sheet['K30'].value, sheet['G30'].value],
    [sheet['BG31'].value, sheet['BC31'].value, sheet['AY31'].value, sheet['AU31'].value, sheet['AQ31'].value,
     sheet['AM31'].value, sheet['AI31'].value, sheet['AE31'].value, sheet['AA31'].value, sheet['W31'].value,
     sheet['S31'].value, sheet['O31'].value,
     sheet['K31'].value, sheet['G31'].value],
]
y_hn = []
for i in range(0, 18):
    y_hn.append(np.array(ZF_RATE_HN[i]))
y_xn = []
for i in range(0, 7):
    y_xn.append(np.array(ZF_RATE_XN[i]))

x1 = np.array(DATE)  # x_日期
fig = plt.figure(figsize=(16, 9))
plt.suptitle(str(sheet['E3'].value)[5:10][0:2]+'月'+str(sheet['E3'].value)[5:10][3:5]+'日'+'分拨直分占比曲线（二）', fontsize=18)
plt.axis('off')
# 子图1
ax1 = fig.add_subplot(221)
plt.plot(x1, y_hn[0], 'blue', label='晋江')
plt.plot(x1, y_hn[1], 'red', label='福州')
plt.plot(x1, y_hn[2], 'purple', label='三明')
plt.plot(x1, y_hn[3], 'green', label='厦门')
plt.plot(x1, y_hn[4], 'teal', label='南宁')
plt.plot(x1, y_hn[5], 'brown', label='柳州')
ax1.legend(loc='upper center', bbox_transform=ax1.transAxes, ncol=3, frameon=False)  # 设置图例
lim_y_hn = [sort(y_hn[0])[13], sort(y_hn[1])[13], sort(y_hn[2])[13], sort(y_hn[3])[13], sort(y_hn[4])[13], sort(y_hn[5])[13]]
lim_y_hn.sort()
# plt.xlabel('日期')
plt.ylabel('直分占比')
plt.title('华南（6/18）', fontsize=14)
plt.ylim(0, lim_y_hn[5] + 0.15)  # y轴取值范围
ax1.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
texts = [plt.text(x1[13], y_hn[i][13], '%.2f%%' % (y_hn[i][13] * 100), fontsize=10, verticalalignment='bottom',
                  horizontalalignment='center') for i in range(0, 6)]
# adjust_text(texts,
#             arrowprops=dict(
#                 arrowstyle='-',  # 箭头样式
#                 lw=0.5,  # 线宽
#                 color='black')  # 箭头颜色
#             )

# 子图2
ax2 = fig.add_subplot(222)
plt.plot(x1, y_hn[6], 'blue', label='东莞')
plt.plot(x1, y_hn[7], 'red', label='深圳')
plt.plot(x1, y_hn[8], 'purple', label='广州')
plt.plot(x1, y_hn[9], 'green', label='中山')
plt.plot(x1, y_hn[10], 'teal', label='茂名')
plt.plot(x1, y_hn[11], 'brown', label='佛山')
ax2.legend(loc='upper center', bbox_transform=ax1.transAxes, ncol=3, frameon=False)  # 设置图例
lim_y_hn = [sort(y_hn[6])[13], sort(y_hn[7])[13], sort(y_hn[8])[13], sort(y_hn[9])[13], sort(y_hn[10])[13], sort(y_hn[10])[13]]
lim_y_hn.sort()
# plt.xlabel('日期')
plt.ylabel('直分占比')
plt.title('华南（12/18）', fontsize=14)
plt.ylim(0, lim_y_hn[5] + 0.15)  # y轴取值范围
ax2.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
texts = [plt.text(x1[13], y_hn[i][13], '%.2f%%' % (y_hn[i][13] * 100), fontsize=10, verticalalignment='bottom',
                  horizontalalignment='center') for i in range(6, 12)]
# adjust_text(texts,
#             arrowprops=dict(
#                 arrowstyle='-',  # 箭头样式
#                 lw=0.5,  # 线宽
#                 color='black')  # 箭头颜色
#             )
# 子图3
ax3 = fig.add_subplot(223)
plt.plot(x1, y_hn[12], 'blue', label='长沙')
plt.plot(x1, y_hn[13], 'brown', label='衡阳')
plt.plot(x1, y_hn[14], 'purple', label='南昌')
plt.plot(x1, y_hn[15], 'green', label='赣州')
plt.plot(x1, y_hn[16], 'red', label='海口')
plt.plot(x1, y_hn[17], 'teal', label='揭阳')
ax3.legend(loc='upper center', bbox_transform=ax1.transAxes, ncol=3, frameon=False)  # 设置图例
lim_y_hn = [sort(y_hn[12])[13], sort(y_hn[13])[13], sort(y_hn[14])[13], sort(y_hn[15])[13], sort(y_hn[16])[13], sort(y_hn[16])[13]]
lim_y_hn.sort()
plt.xlabel('日期')
plt.ylabel('直分占比')
plt.title('华南（18/18）', fontsize=14)
plt.ylim(0, lim_y_hn[5] + 0.15)  # y轴取值范围
ax3.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
texts = [plt.text(x1[13], y_hn[i][13], '%.2f%%' % (y_hn[i][13] * 100), fontsize=10, verticalalignment='bottom',
                  horizontalalignment='center') for i in range(12, 18)]
# adjust_text(texts,
#             arrowprops=dict(
#                 arrowstyle='-',  # 箭头样式
#                 lw=0.5,  # 线宽
#                 color='black')  # 箭头颜色
#             )
# 子图4
ax1 = fig.add_subplot(224)
plt.plot(x1, y_xn[0], 'blue', label='贵阳')
plt.plot(x1, y_xn[1], 'red', label='成都')
plt.plot(x1, y_xn[2], 'purple', label='南充')
plt.plot(x1, y_xn[3], 'green', label='自贡')
plt.plot(x1, y_xn[4], 'teal', label='昆明')
plt.plot(x1, y_xn[5], 'brown', label='大理')
plt.plot(x1, y_xn[6], 'yellow', label='重庆')
ax1.legend(loc='upper center', bbox_transform=ax1.transAxes, ncol=4)  # 设置图例
lim_y_xn = [sort(y_xn[0])[13], sort(y_xn[1])[13], sort(y_xn[2])[13], sort(y_xn[3])[13], sort(y_xn[4])[13], sort(y_xn[5])[13], sort(y_xn[6])[13]]
lim_y_xn.sort()
plt.xlabel('日期')
plt.ylabel('直分占比')
plt.title('西南（7）', fontsize=14)
plt.ylim(0, lim_y_xn[6] + 0.15)  # y轴取值范围
ax1.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
texts = [plt.text(x1[13], y_xn[i][13], '%.2f%%' % (y_xn[i][13] * 100), fontsize=10, verticalalignment='bottom',
                  horizontalalignment='center') for i in range(0, 7)]
# adjust_text(texts,
#             arrowprops=dict(
#                 arrowstyle='-',  # 箭头样式
#                 lw=0.5,  # 线宽
#                 color='black')  # 箭头颜色
#             )
plt.show()
# plt.savefig(fname='D:\\Desktop\\直分图表\\直分率\\2023'+str(today())[5:7] + str(today())[8:10]+'\\04-华南西南'+str(today())[5:7]+str(today())[8:10]+'.png')
# print('04华南西南'+str(today())[5:7]+str(today())[8:10]+'.png'+'已保存')
# os.system('python 直分曲线（华北-西北-华中-东北）.py')
